﻿@inject NavigationManager NavigationManager
@inject IBlogService Service
@inject NotificationService NotificationService

<Loader OnLoad="@OnLoad" AutoLoad="@true" @ref="loader">
    @if (Data.CurrentPage.TotalCount > 0)
    {
        <PagingList Data="@Data" Column="1" OnUpdatePage="@OnUpdatePage" OnPageTo="@OnPageTo">
            <ItemTemplate>
                <CommentListItem Id="@context"></CommentListItem>
            </ItemTemplate>
            <Header>
                <span>
                    @($"{Data.CurrentPage.TotalCount} {(Data.CurrentPage.TotalCount > 1 ? "comments" : "comment")}")
                </span>
            </Header>
        </PagingList>
    }
</Loader>

<AntDesign.Comment Author="@model.Author" Avatar="@Utils.GetGravatarUrl(model.Email)">
    <ContentTemplate>
        <Form Model="@model" OnFinish="OnSubmit">
            <FormItem Label="Name">
                <Input @bind-Value="@model.Author" />
            </FormItem>
            <FormItem Label="Email">
                <Input @bind-Value="@model.Email" />
            </FormItem>
            <FormItem Label="Link">
                <Input @bind-Value="@model.Link" />
            </FormItem>
            <FormItem Label="Content">
                <TextArea @bind-Value="@model.Content" AutoSize="@true" MinRows="5"></TextArea>
            </FormItem>
            <FormItem>
                <Button HtmlType="submit" Type="@ButtonType.Primary">
                    Reply
                </Button>
            </FormItem>
        </Form>
    </ContentTemplate>
</AntDesign.Comment>

@code {
    // Null for auto detect
    [Parameter]
    public string Uri { get; set; } = "";

    const int PageSize = 10;

    PagingData<string> Data { get; set; }

    private int PageNumber { get; set; } = 0;

    private Loader loader;

    private AcBlog.Data.Models.Comment model = new Data.Models.Comment();

    private async Task OnLoad()
    {
        if (string.IsNullOrEmpty(Uri))
        {
            Uri = NavigationManager.ToBaseRelativePath(NavigationManager.Uri);
        }

        Data = await Service.CommentService.QueryPaging(new CommentQueryRequest
        {
            Uri = Uri,
            Pagination = new Data.Models.Actions.Pagination
            {
                CurrentPage = PageNumber,
                PageSize = PageSize,
            }
        });
    }

    private bool IsSubmitting { get; set; } = false;

    private async Task OnSubmit()
    {
        IsSubmitting = true;

        model.Uri = Uri;
        model.CreationTime = DateTimeOffset.Now;
        model.ModificationTime = DateTimeOffset.Now;

        try
        {
            await Service.CommentService.Create(model);
        }
        catch
        {
            await NotificationService.Error(new NotificationConfig
            {
                Message = "Failed to comment."
            });
        }

        IsSubmitting = false;
        model = new Data.Models.Comment();

        await OnUpdatePage();
    }

    private async Task OnPageTo(int pageNumber)
    {
        PageNumber = pageNumber;
        await OnUpdatePage();
    }

    private async Task OnUpdatePage() => await loader.Load();
}
